{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import heapq\n",
    "import random\n",
    "random.seed(123)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 17, 15, 19, 12, 13, 16, 11, 14, 10]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "heap = list(range(10, 20))\n",
    "random.shuffle(heap)\n",
    "heap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "heapq.heapify(heap)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "heapq.heappop(heap)   # 弹出栈顶元素（最小）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "heapq.heappop(heap) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "heapq.heappush(heap, 23)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "heapq.heappop(heap) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Vocab(object):\n",
    "    \"\"\"用来存储词汇，如果用hs，则可以看成一个树结点\"\"\"\n",
    "    \n",
    "    def __init__(self, **kwargs):\n",
    "        \"\"\"\n",
    "        count 词频\n",
    "        index 索引\n",
    "        left  如果用hs，则表示左孩子\n",
    "        right 如果用hs，则表示右孩子\n",
    "        code  叶子结点的编码路径（从根结点到叶子结点的huffman编码）\n",
    "        point 叶子结点的结点路径（从根结点到叶子结点经过的结点索引）\n",
    "        \"\"\"\n",
    "        self.count = 0\n",
    "        self.index = -1\n",
    "        self.left = None\n",
    "        self.right = None\n",
    "        self.code = []\n",
    "        self.point = []\n",
    "        self.__dict__.update(kwargs)\n",
    "        print(self.__dict__)\n",
    "        print(kwargs)\n",
    "\n",
    "    def __lt__(self, vocab):\n",
    "        return self.count < vocab.count\n",
    "\n",
    "    def __str__(self):\n",
    "        fmt = \"<{}>\"\n",
    "        s = \", \".join(['{}:{}'.format(k, v) for k, v in self.__dict__.items() \\\n",
    "                       if not k.startswith('__')])\n",
    "        return fmt.format(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'count': 2, 'index': 1, 'left': None, 'right': None, 'code': [], 'point': []}\n",
      "{'count': 2, 'index': 1, 'left': None, 'right': None}\n"
     ]
    }
   ],
   "source": [
    "a = Vocab(count=2,index=1,left=None,right=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'count': 5, 'index': 0, 'left': None, 'right': None, 'code': [], 'point': []}\n",
      "{'count': 5, 'index': 0, 'left': None, 'right': None}\n"
     ]
    }
   ],
   "source": [
    "b = Vocab(count=5,index=0,left=None,right=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'count': 2, 'index': 1, 'left': None, 'right': None, 'code': [], 'point': []}"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.__dict__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "mappingproxy({'__module__': '__main__',\n",
       "              '__doc__': '用来存储词汇，如果用hs，则可以看成一个树结点',\n",
       "              '__init__': <function __main__.Vocab.__init__(self, **kwargs)>,\n",
       "              '__lt__': <function __main__.Vocab.__lt__(self, vocab)>,\n",
       "              '__str__': <function __main__.Vocab.__str__(self)>,\n",
       "              '__dict__': <attribute '__dict__' of 'Vocab' objects>,\n",
       "              '__weakref__': <attribute '__weakref__' of 'Vocab' objects>})"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Vocab.__dict__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<count:2, index:1, left:None, right:None, code:[], point:[]>\n"
     ]
    }
   ],
   "source": [
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "li = list(range(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(li, reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
